﻿using AntdUI;
using BST250301.Models;
using BST250301TestStand.Models;
using BST250301TestStand.Models.Helpers;
using MathNet.Numerics;
using Mysqlx.Crud;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace BST250301.Views
{
    internal partial class ProjectSub : UserControl
    {
        private AntdUI.Window _window;
        AntList<ProjectItem> antList = new AntList<ProjectItem>();
        private ProjectItem _item;
        internal bool submit;
        internal ProjectSub(AntdUI.Window window, ProjectItem item, AntList<ProjectItem> list)
        {
            this._window = window;
            this._item = item;
            this.antList = list;
            InitializeComponent();
            InitTableColumns();
            BindEventHandler();
            InitData(list);
        }

        private void BindEventHandler()
        {
            table_base.CellButtonClick += Table_base_CellButtonClick;
            table_base.CellDoubleClick += Table_base_CellClick;
        }
        private void InitData(AntList<ProjectItem> list)
        {
            table_base.Binding(list);
            table_base.Bordered = true;
        }
        private void Table_base_CellClick(object sender, TableClickEventArgs e)
        {
            int cellindex = table_base.SelectedIndex - 1;
            this._item = antList[cellindex];
            var form = new ProjectEdit(_window, this._item);// { Size = new Size(800, 500) };
            AntdUI.Modal.open(new AntdUI.Modal.Config("", form, TType.None)
            {
                BtnHeight = 0,
            });
            if (form.submit)
            {
                antList[cellindex] = this._item;
            }
        }
        private void InitTableColumns()
        {
            table_base.Columns = new ColumnCollection() {
                new AntdUI.Column("Name", "项目名称",ColumnAlign.Center),
                new AntdUI.Column("ConfirmWay", "工序确认",ColumnAlign.Center),
                new AntdUI.Column("TemperatureOil", "油温",ColumnAlign.Center),
                 new AntdUI.Column("CFStatus", "CF状态",ColumnAlign.Center),
                new AntdUI.Column("CRStatus", "CR状态",ColumnAlign.Center),
                new AntdUI.Column("CPTOStatus", "CPTO状态",ColumnAlign.Center),
                new AntdUI.Column("MasterGearPosition", "主挡位",ColumnAlign.Center),
                new AntdUI.Column("SlaveGearPosition", "副挡位",ColumnAlign.Center),
                new AntdUI.Column("RotateSeeped", "发动机转速",ColumnAlign.Center),
                //new AntdUI.Column("TestCondition", "前置条件",ColumnAlign.Center),
                new AntdUI.Column("CellLinks", "选择",ColumnAlign.Center),

            };
        }
        private void Table_base_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            var buttontext = e.Btn.Text;

            if (e.Record is ProjectItem item)
            {
                switch (buttontext)
                {
                    //暂不支持进入整行编辑，只支持指定单元格编辑，推荐使用弹窗或抽屉编辑整行数据
                    case "选中":
                        try
                        {
                            int cellindex = table_base.SelectedIndex - 1;
                            var selected = antList[cellindex];
                            this._item.Name = selected.Name;
                            this._item.ConfirmWay = selected.ConfirmWay;
                            this._item.Description = selected.Description;
                            this._item.Code = selected.Code;
                            this._item.TemperatureOil = selected.TemperatureOil;
                            this._item.MasterGearPosition = selected.MasterGearPosition;
                            this._item.SlaveGearPosition = selected.SlaveGearPosition;
                            this._item.RotateSpeed = selected.RotateSpeed;
                            this._item.TestCondition = selected.TestCondition;
                            this._item.ListRecordData =selected.ListRecordData;
                            submit = true;
                            this.Dispose();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show($"格式有错误，请检查后再试:{ex.Message}");
                        }
                        break;
                }
            }
        }

        private void buttonAddPro_Click(object sender, EventArgs e)
        {
            DialogResult confirmResult = MessageBox.Show("确认加入选中项目？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (confirmResult != DialogResult.Yes)
                return;
            try
            {
                //this._item.Probability = float.Parse(selectProbability.Text);
                this._item.Description = "testtetewtwrwetw";
                submit = true;
                this.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"格式有错误，请检查后再试:{ex.Message}");
            }
        }

        private void buttonDEL_Click(object sender, EventArgs e)
        {
            submit = false;
            this.Dispose();
        }

        private void buttonAddPro_Click_1(object sender, EventArgs e)
        {
            var item = new ProjectItem();
            if (antList.Count > 0)
            {
                var last = antList.Last();
                //item =last.Copy();
                item = Copy.ProjectItemCopyJson(last);
            }
            var form = new ProjectEdit(_window,item);// { Size = new Size(500, 350) };
            AntdUI.Modal.open(new AntdUI.Modal.Config("", form, TType.None)
            {
                BtnHeight = 0,
            });
            if (form.submit)
            {
                antList.Add(item);
                var ret = DB.SaveProject(item, MainWindow.configapp.LoginUser);
                if (ret)
                    MessageBox.Show($"用户【{MainWindow.configapp.LoginUser}】插入到项目库一条【{item.Name}】记录。");
            }
        }
    }
}
